#include "historymessagewidget.h"

/////////////////////////////////////////
/// 历史消息区域
/////////////////////////////////////////

HistoryMessageWidget::HistoryMessageWidget(QWidget* parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setFixedSize(600, 600);
    this->setWindowTitle("历史消息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QDialog { background-color:rgb(255, 255, 255); }");

    // 布局管理器
    QGridLayout* glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(20, 20, 20, 20);
    glayout->setAlignment(Qt::AlignTop);
    this->setLayout(glayout);

    // 单选框
    QString style = "QRadioButton { color:rgb(50, 50, 50); font-size:15px; }";

    QRadioButton* keyRadioBtn = new QRadioButton();
    keyRadioBtn->setText("按关键字查询");
    keyRadioBtn->setStyleSheet(style);
    keyRadioBtn->setChecked(true); // 默认按关键字查询

    QRadioButton* timeRadioBtn = new QRadioButton();
    timeRadioBtn->setText("按时间查询");
    timeRadioBtn->setStyleSheet(style);

    // 搜索框
    QLineEdit* lineEdit = new QLineEdit();
    lineEdit->setFixedHeight(40);
    lineEdit->setPlaceholderText("请输入关键字");
    lineEdit->setStyleSheet("QLineEdit { color:rgb(50, 50, 50); border-radius:10px; "
                            "border:none; font-size:15px; padding-left:10px; "
                            "background-color:rgb(230, 230, 230); }");

    // 搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    searchBtn->setFixedSize(40, 40);
    searchBtn->setIconSize(QSize(20, 20));
    searchBtn->setStyleSheet("QPushButton:hover { background-color:rgb(210, 210, 210); }"
                             "QPushButton:pressed { background-color:rgb(200, 200, 200); }"
                             "QPushButton { background-color:rgb(230, 230, 230); "
                             "border:none; border-radius:10px; }");

    // 时间
    QLabel* begTag = new QLabel("开始时间");
    begTag->setStyleSheet("QLabel { font-size:15px; color:rgb(50, 50, 50); }");
    QLabel* endTag = new QLabel("结束时间");
    endTag->setStyleSheet("QLabel { font-size:15px; color:rgb(50, 50, 50); }");

    QString timeStyle = "QDateTimeEdit { background-color:rgb(230, 230, 230); "
                        "color:rgb(50, 50, 50); }";
    QDateTimeEdit* begTime = new QDateTimeEdit();
    begTime->setFixedHeight(40);
    begTime->setStyleSheet(timeStyle);
    QDateTimeEdit* endTime = new QDateTimeEdit();
    endTime->setFixedHeight(40);
    endTime->setStyleSheet(timeStyle);

    begTag->hide();
    begTime->hide();
    endTag->hide();
    endTime->hide();

    // 历史消息
    initHistoryMessage(glayout);

    // 添加到布局管理器中
    glayout->addWidget(keyRadioBtn, 0, 0, 1, 2);
    glayout->addWidget(timeRadioBtn, 0, 2, 1, 2);
    glayout->addWidget(lineEdit, 1, 0, 1, 8);
    glayout->addWidget(searchBtn, 1, 8, 1, 1);

    // 点击关键字查询/时间查询按钮,显示对应的界面
    connect(keyRadioBtn, &QRadioButton::clicked, this, [=](){
        glayout->removeWidget(begTag);
        glayout->removeWidget(begTime);
        glayout->removeWidget(endTag);
        glayout->removeWidget(endTime);
        begTag->hide();
        begTime->hide();
        endTag->hide();
        endTime->hide();

        glayout->addWidget(lineEdit, 1, 0, 1, 8);
        lineEdit->show();
    });

    connect(timeRadioBtn, &QRadioButton::clicked, this, [=](){
        glayout->removeWidget(lineEdit);
        lineEdit->hide();

        glayout->addWidget(begTag, 1, 0, 1, 1);
        glayout->addWidget(begTime, 1, 1, 1, 3);
        glayout->addWidget(endTag, 1, 4, 1, 1);
        glayout->addWidget(endTime, 1, 5, 1, 3);
        begTag->show();
        begTime->show();
        endTag->show();
        endTime->show();
    });

}

void HistoryMessageWidget::initHistoryMessage(QGridLayout *glayout)
{
    QScrollArea* area = new QScrollArea();
    area->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width:2px; }");
    area->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height:0px; }");
    area->setStyleSheet("QScrollArea { border:none; }");
    area->setWidgetResizable(true);
    glayout->addWidget(area, 2, 0, 1, 9);

    QWidget* container = new QWidget();
    container->setStyleSheet("QWidget { background-color:rgb(255, 255, 255); }");
    area->setWidget(container);

    vlayout = new QVBoxLayout();
    vlayout->setSpacing(20);
    vlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->setAlignment(Qt::AlignLeft);
    container->setLayout(vlayout);

    // 测试数据
#if TEST_F
    for(int i = 0;i < 20;i ++) {
        model::UserInfo sender;
        sender.avatar = QIcon(":/resource/image/default_avatar.png");
        sender.nickname = "路飞" + QString::number(i);
        model::Message msg = model::Message::makeMessage(model::TEXT_TYPE, "", sender, ("成为海贼王的男人成为海贼王的男人成为海贼王的男人成为海贼王的男人成为海贼王的男人成为海贼王的男人成为海贼王的男人" + QString::number(i)).toUtf8(), "");
        this->addHistoryMessage(msg);
    }
#endif
}

void HistoryMessageWidget::addHistoryMessage(model::Message msg)
{
    HistoryItem* item = HistoryItem::makeHistoryMessage(msg);
    vlayout->addWidget(item);
}

void HistoryMessageWidget::clear()
{

}

/////////////////////////////////////////
/// 历史消息 Item
/////////////////////////////////////////

HistoryItem::HistoryItem(model::Message msg)
{
    this->setFixedWidth(500);

    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(5);
    layout->setVerticalSpacing(5);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(layout);

    // 头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40, 40);
    avatarBtn->setIcon(msg.sender.avatar);
    avatarBtn->setIconSize(QSize(40, 40));

    // 昵称 + 时间
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(msg.sender.nickname + " | " + msg.time);
    nameLabel->setStyleSheet("QLabel { font-size:10px; font-weight:800; color:rgb(50, 50, 50); }");

    // 消息主体
    QLabel* msgLabel = new QLabel();
    msgLabel->setText(msg.content);
    msgLabel->setStyleSheet("QLabel { font-size:12px; color:rgb(50, 50, 50); }");
    msgLabel->setFixedWidth(400);
    msgLabel->setWordWrap(true);

    // 添加到布局管理器
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(msgLabel, 1, 1, 1, 1);
}

HistoryItem *HistoryItem::makeHistoryMessage(model::Message msg)
{
    HistoryItem* item = nullptr;
    if(msg.type == model::TEXT_TYPE) {
        item = new HistoryItem(msg);
    } else if(msg.type == model::IMAGE_TYPE) {

    } else if(msg.type == model::FILE_TYPE) {

    } else if(msg.type == model::SPEECH_TYPE) {

    } else {
        LOG() << "未知的文件类型";
    }
    return item;
}
